var app = getApp();
var util = require('../../utils/util.js')
const config = require('../../utils/config.js');
var my = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {


    //接口地址
    HotActivityList: "activity/hotList",
    bannerList:"banner/list",
    loginByCode:"users/loginByCode",

    sliderList: [],
    HotactList:[],

    location: '',
    county: '',

    today:"",
    inTheaters: {},
    containerShow: true,
    weatherData: '' ,
    air:'',
    dress:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    //自动登录
    var that=this;
      wx.login({
        success: function (res) {
          console.log(res.code)
          wx.showToast({
            title: '自动登录中',
            icon: 'loading',
            duration: 500
          }) 
          if (res.code) {
            //发起网络请求
            my.request({
              url: that.data.loginByCode,
              data: {
                code: res.code
              },
              success: function(res){
                console.log("login")
                console.log(res)

                //将token保存到程序内
                var token = res.data.wxToken
                wx.setStorageSync('token', token)

                var app = getApp();
                var cookie = res.header['Set-Cookie']
                if (cookie != null) {
                  app.globalData.app_cookie = cookie;
                }   
                wx.showToast({
                  title: '登录成功',
                  icon: 'loading',
                  duration: 500
                })
                that.getBannerList();
                that.getActivityList();      
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
            wx.showToast({
              title: '登录失败',
              icon: 'loading',
              duration: 500
            })
          }
        }
      })

    app.globalData.day = util.formatTime(new Date()).split(' ')[0];
    that.setData({
      today: app.globalData.day  //更新当前日期
    });

   
    
   

    // var inTheatersUrl = app.globalData.doubanBase +
    //   "/v2/movie/in_theaters" + "?start=0&count=6";
    // this.getMovieListData(inTheatersUrl, "inTheaters", "热门活动");
  },

 
  // //定位当前城市的函数
  // getLocation: function () {
  //  // console.log("正在定位城市");
  //   var that = this;
  //   wx.getLocation({
  //     type: 'wgs84',
  //     success: function (res) {
  //       //当前的经度和纬度
  //       let latitude = res.latitude
  //       let longitude = res.longitude
  //       wx.request({
  //         url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${config.key}`,
  //         success: res => {
  //           //console.log(res);
  //           app.globalData.defaultCity = res.data.result.ad_info.city;
  //           app.globalData.defaultCounty=res.data.result.ad_info.district;
  //           that.setData({
  //             location: app.globalData.defaultCity,
  //             county: app.globalData.defaultCounty
  //           });
  //          // console.log(app.globalData.defaultCity);
  //          // console.log(app.globalData.defaultCounty);
  //           //that.getWeather();
  //           that.getAir();
  //         }
  //       })
  //     }
  //   })
  // },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      location: app.globalData.defaultCity,
      county: app.globalData.defaultCounty
    });
  
  },

  // jump:function(){
  //   console.log("dsad")
  //   wx.navigateTo({
  //     url: '../switchcity/switchcity'
  //   });

  // },
  
  //转换banner
  swiTab: function (e) {
    var sliederList = this.data.sliderList;
    var i, item;
    for (i = 0; item = sliederList[i]; ++i) {
      item.selected = e.detail.current == i;
    }
    this.setData({
      sliderList: sliederList
    });
  }, 


  //获取banner列表
  getBannerList:function(){
    var that=this;
    my.request({
      url:that.data.bannerList,
      //data: params,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      }, 
      success: function(res){
        
        //Object.assign(res,{selected:true});

        res.data.data.forEach(item=>{
          if(item.index==0){
            item.selected=true
          }else{
            item.selected=false
          }
        })

        console.log(res)
         that.setData({
          "sliderList":res.data.data
         })
      }

    })

  },

  //获取热门活动列表
  getActivityList:function(){
    var that=this;
    my.request({
      url:that.data.HotActivityList,
      //data: params,
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      }, 
      success: function(res){
        
        console.log(res)
         that.setData({
          "HotactList":res.data.data
         })
      }

    })

  },

  //点击热门活动
  HotDetail: function(e){
    var hid=e.currentTarget.dataset.hid
    console.log(e.currentTarget.dataset.hid)
    wx.navigateTo({
      url:'../detail/detail?hid='+hid
    });
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})